﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Bll
{
    public class BLLCompanyCjhSum:BaseBLL
    {
        private ITableXtCompany xt_company_DAL = null;
        private ITableXtSbJd xt_sb_jd_DAL = null;
        private ITableXtCompany_sb_info xt_company_sb_DAL = null;
        private ITable_xt_waterproperty xt_waterproperty_DAL = null;
        private ITableXtDelegateBank xt_delegate_bank_DAL = null;
        private ITableXtBaseBankinfo xt_base_bankinfo_DAL = null;
        
        public BLLCompanyCjhSum()
        {
            xt_company_DAL = Reflect<ITableXtCompany>.Create("DAL_table_xt_company", "Willson.JsbManager.Dal");
            xt_base_bankinfo_DAL = Reflect<ITableXtBaseBankinfo>.Create("DAL_table_xt_base_bankinfo", "Willson.JsbManager.Dal");
            xt_sb_jd_DAL = Reflect<ITableXtSbJd>.Create("DAL_table_xt_sb_jd", "Willson.JsbManager.Dal");
            xt_company_sb_DAL = Reflect<ITableXtCompany_sb_info>.Create("DAL_table_xt_company_sb_info", "Willson.JsbManager.Dal");
            xt_waterproperty_DAL = Reflect<ITable_xt_waterproperty>.Create("DAL_table_xt_waterproperty", "Willson.JsbManager.Dal");
            xt_delegate_bank_DAL = Reflect<ITableXtDelegateBank>.Create("DAL_table_xt_delegate_bank", "Willson.JsbManager.Dal");
        }

        public BindingList<BECompanyDeleGateInfo> GetCompanyDeleGateInfoList(int year, int jd)
        {
            BindingList<BECompanyDeleGateInfo> list = new BindingList<BECompanyDeleGateInfo>();

            Hashtable hash = new Hashtable();

            hash.Add("sb_year", year);
            hash.Add("sb_jd", jd);

            IEnumerator<TableXtSbJdEntity> ator = xt_sb_jd_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            hash.Clear();

            while (ator.MoveNext())
            {
                if (ator.Current.Jd_scjh > 0)
                {
                    hash.Add("company_id", ator.Current.Company_id);

                    TableXtDelegateBankEntity deletegate_bank = xt_delegate_bank_DAL.Find(hash);

                    hash.Clear();

                    if (deletegate_bank == null) continue;

                    TableXTBaseBankInfoEntity bankinfo = xt_base_bankinfo_DAL.FindByID(deletegate_bank.Bank_id);
                    BECompanyDeleGateInfo info = new BECompanyDeleGateInfo();

                    //info.DeleGateCode = "DI" + deletegate_bank.Delegate_code;
                    info.CompanyId = ator.Current.Company_id;
                    info.DeleGateCode = deletegate_bank.Delegate_code;
                    info.CompanyName = deletegate_bank.Name;
                    info.Account = deletegate_bank.Account;
                    info.Cjh = ator.Current.Jd_scjh;
                    info.BankName = bankinfo.Name;
                    info.xxRmb = string.Format("{0:F2}", ator.Current.Jd_scjh * GetMaxDj(ator.Current.Company_id));
                    info.Rmb = Rmb.CmycurD(info.xxRmb);

                    list.Add(info);

                }
            }


            return list;
        }
        
        public List<BECompanyLetterPrint> GetCompanyLetterPrintInfoList(int year, int jd)
        {
            List<BECompanyLetterPrint> list = new List<BECompanyLetterPrint>();

            Hashtable hash = new Hashtable();

            hash.Add("sb_year", year);
            hash.Add("sb_jd", jd);

            IEnumerator<TableXtSbJdEntity> ator = xt_sb_jd_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                if (ator.Current.Jd_scjh > 0)
                {
                    TableXtCompanyEntity company_entity = xt_company_DAL.FindByID(ator.Current.Company_id);

                    BECompanyLetterPrint info = new BECompanyLetterPrint();
                    info.m_PostCode = company_entity.Code;
                    info.m_Address = company_entity.Address;
                    info.m_Sjr = company_entity.Name + company_entity.Sjr + "收";

                    list.Add(info);
                }
            }

            return list;
        }

        public List<BECompanyCjhLetter> GetCompanyCjhLetterInfo(int year, int jd)
        {
            decimal ldc_money = 0.00m;
            decimal ldc_all_money = 0.00m;
            decimal ldc_max_dj = 0.00m;
            int sb_ht_index = 1;
            int jd_scjhsl = 0;
            int jd_jhsl = 0;            

            List<BECompanyCjhLetter> list = new List<BECompanyCjhLetter>();

            Hashtable hash = new Hashtable();

            hash.Add("sb_year", year);
            hash.Add("sb_jd", jd);

            IEnumerator<TableXtSbJdEntity> ator = xt_sb_jd_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                if (ator.Current.Jd_scjh > 0)
                {
                    sb_ht_index = 1;
                    jd_scjhsl = 0;
                    jd_jhsl = 0;
                    ldc_money = 0.00m;
                    ldc_all_money = 0.00m;
                                        
                    
                    BECompanyCjhLetter info = new BECompanyCjhLetter();
                    Type type = info.GetType();

                    TableXtCompanyEntity company_entity = xt_company_DAL.FindByID(ator.Current.Company_id);

                    hash.Clear();
                    hash.Add("company_id", ator.Current.Company_id);

                    IEnumerator<TableXtCompanySbInfoEntity> sb_ator = xt_company_sb_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

                    //处理合同号
                    while (sb_ator.MoveNext())
                    {
                        if (sb_ator.Current.Remove == 1) continue;

                        type.GetField("ht_code_"+sb_ht_index.ToString()).SetValue(info, sb_ator.Current.Ht_code);

                        sb_ht_index++;
                    }

                    ///////////////

                    if (ator.Current.Jd_jh_begin > 0) 
                        info.jd_jh = ator.Current.Jd_jh_begin.ToString();
                    else
                        info.jd_jh = ator.Current.Jd_jh.ToString();

                    info.jd_ysl = ator.Current.Jd_sum.ToString();

                    //处理水量
                    jd_scjhsl = ator.Current.Jd_scjh;
                    jd_jhsl = Convert.ToInt32(info.jd_jh);
                    ldc_max_dj = GetMaxDj(ator.Current.Company_id);

                    if (Convert.ToInt32(jd_jhsl * 0.2) > jd_scjhsl)
                    {
                        info.sl_1 = jd_scjhsl.ToString();
                        info.bfb_1 = "20%";
                        info.bs_1 = "1";
                        info.dj_1 = string.Format("{0:C2}",ldc_max_dj);

                        ldc_money = jd_scjhsl * ldc_max_dj;

                        info.money_1 = string.Format("{0:C2}",ldc_money);

                        ldc_all_money = ldc_all_money + ldc_money;

                    }
                    else
                    {
                        info.sl_1 = Convert.ToInt32(jd_jhsl * 0.2).ToString();
                        info.bfb_1 = "20%";
                        info.bs_1 = "1";
                        info.dj_1 = string.Format("{0:C2}",ldc_max_dj);

                        ldc_money = Convert.ToInt32(jd_jhsl * 0.2) * ldc_max_dj;

                        info.money_1 = string.Format("{0:C2}",ldc_money);
                        ldc_all_money = ldc_all_money + ldc_money;

                        jd_scjhsl =  jd_scjhsl - Convert.ToInt32(jd_jhsl * 0.2);

                        if (Convert.ToInt32(jd_jhsl * 0.5) - Convert.ToInt32(jd_jhsl * 0.2) > jd_scjhsl)
                        {
                            info.sl_2 = jd_scjhsl.ToString();
                            info.bfb_2 = "50%";
                            info.bs_2 = "2";
                            info.dj_2 = string.Format("{0:C2}",ldc_max_dj);

                            ldc_money = jd_scjhsl * ldc_max_dj * 2;

                            info.money_2 = string.Format("{0:C2}",ldc_money);

                            ldc_all_money = ldc_all_money + ldc_money;

                        }
                        else
                        {
                            info.sl_2 = (Convert.ToInt32(jd_jhsl * 0.5) - Convert.ToInt32(jd_jhsl * 0.2)).ToString();
                            info.bfb_2 = "50%";
                            info.bs_2 = "2";
                            info.dj_2 = string.Format("{0:C2}",ldc_max_dj);

                            ldc_money = (Convert.ToInt32(jd_jhsl * 0.5) - Convert.ToInt32(jd_jhsl * 0.2)) * ldc_max_dj * 2;
                            info.money_2 = string.Format("{0:C2}",ldc_money);

                            ldc_all_money = ldc_all_money + ldc_money;

                            jd_scjhsl = jd_scjhsl - (Convert.ToInt32(jd_jhsl * 0.5) - Convert.ToInt32(jd_jhsl * 0.2));

                            info.sl_3 = jd_scjhsl.ToString();
                            info.bfb_3 = ">50%";
                            info.bs_3 = "3";
                            info.dj_3 = string.Format("{0:C2}",ldc_max_dj);

                            ldc_money = jd_scjhsl * ldc_max_dj * 3;

                            info.money_3 = string.Format("{0:C2}",ldc_money);

                            ldc_all_money = ldc_all_money + ldc_money;

                        }


                    }

                    info.money_all = string.Format("{0:C2}",ldc_all_money);
                    info.company_name = company_entity.Name;

                    list.Add(info);

                }
            }

            return list;
        }

        public decimal GetMaxDj(int company_id)
        {
            decimal max = 0.0m;

            Hashtable hash = new Hashtable();
            hash.Add("company_id", company_id);

            IEnumerator<TableXtCompanySbInfoEntity> sb_ator = xt_company_sb_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (sb_ator.MoveNext())
            {
                Table_xt_waterproperty_entity wt_entity = xt_waterproperty_DAL.FindByID(sb_ator.Current.Wp_id);

                if (wt_entity.Money > max)
                    max = wt_entity.Money;
            }

            return max;
        }

        public List<BECompanyCjhAllInfo> GetCompanyCjhAllInfo(int year, int jd)
        {
            List<BECompanyCjhAllInfo> list = new List<BECompanyCjhAllInfo>();

            Hashtable hash = new Hashtable();

            hash.Add("sb_year", year);
            hash.Add("sb_jd", jd);

            xt_sb_jd_DAL.SetSqlSort(" order by jd_scjh DESC ");
            IEnumerator<TableXtSbJdEntity> ator = xt_sb_jd_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                if (ator.Current.Jd_scjh > 0)
                {
                    TableXtCompanyEntity company_entity = xt_company_DAL.FindByID(ator.Current.Company_id);

                    BECompanyCjhAllInfo info = new BECompanyCjhAllInfo();

                    info.Company_name = company_entity.Name;
                    info.Company_address = company_entity.Address;
                    info.Company_tel = company_entity.Telphone;

                    if (ator.Current.Jd_jh_begin > 0)
                        info.Jd_jh = ator.Current.Jd_jh_begin;
                    else
                        info.Jd_jh = ator.Current.Jd_jh;

                    info.Jd_ysl = ator.Current.Jd_sum;
                    info.Jd_cjh = ator.Current.Jd_scjh;
                    info.Jd_old_jh = ator.Current.Jd_jh;
                    info.Jd_jh_begin = ator.Current.Jd_jh_begin;

                    list.Add(info);

                }
            }

            xt_sb_jd_DAL.SetDefaultSqlSort();

            return list;
        }
    }
}
